package com.icoding.trangphuc.utils

import java.security.MessageDigest;

import com.sun.xml.internal.ws.client.dispatch.MessageDispatch;

class Encryption {
	
	/**
	 * Parse String into MD5 Encrypt
	 * @param str
	 * @return
	 */
	static String md5(String str){
		MessageDigest md5 = MessageDigest.getInstance("md5")
		md5.update(str.getBytes())
		byte[] byteData = md5.digest()
		// Convert String to hex format
		StringBuffer hashStr = new StringBuffer()
		for(i in byteData){
			hashStr.append(Integer.toString((i&0xff)+ 0x100,16).substring(1))
		}
		return hashStr.toString();
	}
	
	public static final String SHA_1 = "SHA-1"
	public static final String SHA_128 = "SHA-128"
	public static final String SHA_256 = "SHA-256"
	public static final String SHA_512 = "SHA-512"
	
	/**
	 * @param str
	 * @param type. We choose type of SHA below
	 * @return
	 */
	static String shaEncryption(String str,String type){
		MessageDigest encrypt = MessageDigest.getInstance(type);
		encrypt.update(str.getBytes());
		byte[] byteArray = encrypt.digest();
		StringBuffer hashStr = new StringBuffer();
		for(i in byteArray){
			hashStr.append(Integer.toString((i&0xff)+ 0x100,16).substring(1))
		}
		return hashStr.toString();
	}
}
